<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\CreateIntegralLogRequest;
use App\Http\Requests\UpdateIntegralLogRequest;
use App\Models\IntegralLog;
use App\Repositories\IntegralLogRepository;
use App\Http\Controllers\AppBaseController;
use App\User;
use Illuminate\Http\Request;
use Flash;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

class IntegralLogController extends AppBaseController
{
    /** @var  IntegralLogRepository */
    private $integralLogRepository;

    public function __construct(IntegralLogRepository $integralLogRepo)
    {
        $this->integralLogRepository = $integralLogRepo;
    }

    /**
     * Display a listing of the IntegralLog.
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->integralLogRepository->pushCriteria(new RequestCriteria($request));
        $nickname = request('nickname');
        $model = new IntegralLog();
        if($nickname){
            $user_ids = User::where('nickname','like',"%$nickname%")->pluck('id');
            $model = $model->whereIn('user_id', $user_ids->toArray());
        }
        $integralLogs = $model->orderBy('id','desc')->paginate(20);
        return view('integral_logs.index')
            ->with('integralLogs', $integralLogs)->with('request', $request->all());
    }

    /**
     * Show the form for creating a new IntegralLog.
     *
     * @return Response
     */
    public function create()
    {
        return view('integral_logs.create');
    }

    /**
     * Store a newly created IntegralLog in storage.
     *
     * @param CreateIntegralLogRequest $request
     *
     * @return Response
     */
    public function store(CreateIntegralLogRequest $request)
    {
        $input = $request->all();

        $integralLog = $this->integralLogRepository->create($input);

        Flash::success('Integral Log saved successfully.');

        return redirect(route('integralLogs.index'));
    }

    /**
     * Display the specified IntegralLog.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $integralLog = $this->integralLogRepository->findWithoutFail($id);

        if (empty($integralLog)) {
            Flash::error('Integral Log not found');

            return redirect(route('integralLogs.index'));
        }

        return view('integral_logs.show')->with('integralLog', $integralLog);
    }

    /**
     * Show the form for editing the specified IntegralLog.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        $integralLog = $this->integralLogRepository->findWithoutFail($id);

        if (empty($integralLog)) {
            Flash::error('Integral Log not found');

            return redirect(route('integralLogs.index'));
        }

        return view('integral_logs.edit')->with('integralLog', $integralLog);
    }

    /**
     * Update the specified IntegralLog in storage.
     *
     * @param  int              $id
     * @param UpdateIntegralLogRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateIntegralLogRequest $request)
    {
        $integralLog = $this->integralLogRepository->findWithoutFail($id);

        if (empty($integralLog)) {
            Flash::error('Integral Log not found');

            return redirect(route('integralLogs.index'));
        }

        $integralLog = $this->integralLogRepository->update($request->all(), $id);

        Flash::success('Integral Log updated successfully.');

        return redirect(route('integralLogs.index'));
    }

    /**
     * Remove the specified IntegralLog from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $integralLog = $this->integralLogRepository->findWithoutFail($id);

        if (empty($integralLog)) {
            Flash::error('Integral Log not found');

            return redirect(route('integralLogs.index'));
        }

        $this->integralLogRepository->delete($id);

        Flash::success('Integral Log deleted successfully.');

        return redirect(route('integralLogs.index'));
    }
}
